import Snake from './Snake'
import Food from './Food'
import ScorePanel from './ScorePanel'

export default class Game {
  snake: Snake
  food: Food
  scorePanel: ScorePanel
  direction: string = ''
  isLive: boolean = true

  constructor() {
    this.snake = new Snake()
    this.food = new Food()
    this.scorePanel = new ScorePanel()

    this.init()
  }

  init = () => {
    document.addEventListener('keydown', this.handleKeyDown)
    this.snakeRun()
  }

  handleKeyDown = (event: KeyboardEvent) => {
    this.direction = event.key
  }

  snakeRun = () => {
    let X = this.snake.X
    let Y = this.snake.Y

    switch (this.direction) {
      case 'ArrowUp':
      case 'Up':
        Y -= 10
        break
      case 'ArrowDown':
      case 'Down':
        Y += 10
        break
      case 'ArrowLeft':
      case 'Left':
        X -= 10
        break
      case 'ArrowRight':
      case 'Right':
        X += 10
        break
    }

    this.checkEat(X, Y)

    try {
      this.snake.X = X
      this.snake.Y = Y
    } catch (e: any) {
      alert(e.message)
      this.isLive = false
    }

    this.isLive && setTimeout(this.snakeRun, 200 - (this.scorePanel.level - 1) * 10)
  }

  checkEat = (X: number, Y: number) => {
    if (X === this.food.X && Y === this.food.Y) {
      this.food.changePosition()
      this.scorePanel.addScore()
      this.snake.addBody()
    }
  }
}
